#include "dlist.h"

/**
 * double link list init
 * @param l: list pointer
 * @return: void
 */
void mx_dlist_init (mx_dlist_pt l)
{
    l->prev = l;
    l->next = l;
}

/**
 * insert the node before the specified double list node
 * @param tl: target list pointer
 * @param l: list pointer
 * @return: void
 */
void mx_dlist_insert_before (mx_dlist_pt tl, mx_dlist_pt l)
{
    tl->prev->next = l;
    l->prev = tl->prev;
    l->next = tl;
    tl->prev = l;
}

/**
 * insert the node after the specified double list node
 * @param tl: target double list pointer
 * @param l: double list pointer
 * @return: void
 */
void mx_dlist_insert_after (mx_dlist_pt tl, mx_dlist_pt l)
{
    tl->next->prev = l;
    l->next = tl->next;
    l->prev = tl;
    tl->next = l;
}

/**
 * remove the specified double list node
 * @param: the double list pointer
 * @return: void
 */
void mx_dlist_remove (mx_dlist_pt l)
{
    l->prev->next = l->next;
    l->next->prev = l->prev;
    l->prev = l;
    l->next = l;
}

/**
 * get the count of double list
 * @param: the double list pointer
 * @return: the count of double list
 */
mx_usize_t mx_dlist_count (mx_dlist_pt tl)
{
    mx_usize_t count = 0;
    if (tl != mx_null)
    {
        mx_dlist_foreach(tl, l, {
            count ++;
        });
    }
    return count;
}
